{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "#单链表\n",
    "class ListNode:\n",
    "    def __init__(self, val=None, next=None):\n",
    "        self.data = val\n",
    "        self.next = next\n",
    "class LinkedList:\n",
    "    def __init__(self):\n",
    "        self.dummy_head = ListNode()\n",
    "        self.size = 0\n",
    "        \n",
    "    def create(self, List):\n",
    "        for val in List:\n",
    "            last_node = self.dummy_head\n",
    "            while last_node.next:\n",
    "                last_node = last_node.next\n",
    "            last_node.next = ListNode(val)\n",
    "            self.size +=1\n",
    "        \n",
    "    def print(self):\n",
    "        cur = self.dummy_head\n",
    "        while cur:\n",
    "            print(cur.data, end=\" -> \")\n",
    "            cur = cur.next\n",
    "        print(\"None\")\n",
    "        print(self.size)\n",
    "        \n",
    "    def algorithm1_1(self, n):                  # 双指针法：一个指向删除节点前一个节点，一个指向链表结尾None（用于确定前一指针位置）\n",
    "        pre = self.dummy_head\n",
    "        while self.movebyN(pre, n).next:      # 判断终止条件：当指针经过movebyN变化后next指向None\n",
    "            pre = pre.next\n",
    "        pre.next = pre.next.next              # 下一个节点为删除前一个节点\n",
    "    def movebyN(self, pre, n):                # 向后移动N个位置\n",
    "        test = pre\n",
    "        for _ in range(n):\n",
    "            test = test.next\n",
    "        return test\n",
    "    \n",
    "    def algorithm1_2(self, n):                 # 双指针法：快慢指针\n",
    "        fast = self.dummy_head\n",
    "        slow = self.dummy_head\n",
    "        for _ in range(n+1):\n",
    "            fast = fast.next\n",
    "        while fast:\n",
    "            fast = fast.next\n",
    "            slow = slow.next\n",
    "        slow.next = slow.next.next\n",
    "    \n",
    "    def algorithm2(self, n):                 # 栈方法：将节点入栈，删除是按放入的反顺序出栈，即可获得删除前一个节点\n",
    "        stack = list()\n",
    "        cur = self.dummy_head\n",
    "        while cur:\n",
    "            stack.append(cur)                # 入栈：每个节点进入，先进的在下方\n",
    "            cur = cur.next        \n",
    "        for _ in range(n):\n",
    "            stack.pop()                      # 出栈：删除节点及后的节点出栈，操作后栈内最上元素为删除位置前一节点\n",
    "        pre = stack[-1]                      # 取出删除位置前一节点\n",
    "        pre.next = pre.next.next\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "LinkedList = LinkedList()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 1, 2, 3, 4]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nums = list(range(5))\n",
    "nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None -> 0 -> 1 -> 2 -> 3 -> 4 -> None\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "LinkedList.create(nums)\n",
    "LinkedList.print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None -> 0 -> 1 -> 2 -> 4 -> None\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "LinkedList.algorithm1_1(2)\n",
    "LinkedList.print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None -> 0 -> 1 -> 4 -> None\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "LinkedList.algorithm1_2(2)\n",
    "LinkedList.print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None -> 0 -> 4 -> None\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "LinkedList.algorithm2(2)\n",
    "LinkedList.print()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
